/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
/*
Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright 
     notice, this list of conditions and the following disclaimer in 
     the documentation and/or other materials provided with the distribution.

  3. The names of the authors may not be used to endorse or promote products
     derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.yisin.ssh2.jsch;

/**
 * Usually not to be used by applications.
 * The implementation of the cryptographic algorithms
 *  in the Diffie-Hellman key exchange.
 *<p>
 * The group in question here is the multiplicative group of
 * the finite field with {@link #setP p} elements. It (or at least
 * a quite large subgroup of it) should be generated by {@link #setG g}.
 *</p>
 *<p>
 * Here is a short view of the algorithm (which would work in any group which
 * we could somehow encode as numbers, by the way):</p>
 * <ul>
 * <li>We generate a random number {@code x},
 *   calculate {@code e = g^x} and send {@link #getE e} to the server.</li>
 * <li>The server generates a random number {@code y}, calculates
 *   {@code f = g^y} and sends us {@link #setF f}.</li>
 * <li>We can know calculate {@link #getK {@code K = f^x}}, while the server
 *    calculates {@code K = e^y} (and since multiplication of integers is
 *    commutative, {@code e^y = (g^x)^y = g^(x*y) = g^(y*x) = (g^y)^x = f^x},
 *    both have the same number.)</li>
 * </ul>
 *<p> The methods of this interface will generally be called in the order
 *  defined here, each once.</p>
 * <p>
 *  The implementing class has to do the random number generation of x and
 *  the modular arithmetic.
 * </p>
 * <p>
 *  The implementing class will be chosen by the
 *  {@linkplain JSch#setConfig configuration option} {@code "dh"},
 *   a default implementation is delivered with the library.
 * </p>
 *
 * 
 * @see <a href="http://tools.ietf.org/html/rfc4253#section-8.1">RFC 4253,
 *   section 8.  Diffie-Hellman Key Exchange</a>
 * @see <a href="http://tools.ietf.org/html/rfc4419">RFC 4419,
 *   Diffie-Hellman Group Exchange for the Secure Shell (SSH)
 *    Transport Layer Protocol</a>
 */
public interface DH{

  /**
   * initializes the algorithm object for a new exchange.
   */
  void init() throws Exception;

  /**
   * Sets the prime number, modulo which the calculations should be done.
   */
  void setP(byte[] p);
  /**
   * Sets the generator of the group. (This will be most
   * often 2 or another small prime.)
   */
  void setG(byte[] g);

  /**
   * Retrieves the value {@code e}, that is the result of
   * {@code g^x mod P}.
   */
  byte[] getE() throws Exception;

  /**
   * Sets the value of {@code f}, that is the result of
   * {@code g^y mod P}
   */
  void setF(byte[] f);

  /**
   * Retrieves the secret number K which was created by the key exchange
   * and will be used to create the key.
   */
  byte[] getK() throws Exception;
}
